<template>
  <div>
    <el-dialog :title="title" :visible.sync="visible" width="500px" append-to-body custom-class="import-dialog">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="名称" prop="cleaningProcessName">
          <el-input v-model="form.cleaningProcessName" placeholder="" style="width: 240px" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align: center">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { debounce } from "@/utils";
import { addCleaner, editCleaner, userInfoCleaner } from '@/api/houseCleaning/cleaner';
import { addPlace, addCheckPoint, editCleaningProcess } from "@/api/houseCleaning/process";
export default {
  components: {

  },
  props: {
    title: {
      default: "新增位置",
    },
    value: {
      default: false,
    },
    data: {
      default: undefined,
    },
    type: {
      default: undefined
    }
  },
  data() {
    return {
      visible: false,
      estateVisible: false,
      form: {
        cleaningProcessName: ''
      },
      rules: {
        cleaningProcessName: [
          { required: true, message: '请输入名称', trigger: 'blur' }
        ]
      },
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 总条数
      total: 0,
      //列表
      list: [],
      //用户列表
      userList: [],
    };
  },
  watch: {
    value(newVal, oldVal) {
      if (newVal) {
        this.init();
      }
      this.visible = newVal; // 监测value的变化，并赋值。
    },
    visible(val) {
      this.$emit("input", val); // 此处监测visible目的为关闭弹窗时，重新更换value值，注意emit的事件一定要为input。
    },
  },
  methods: {
    init() {
      this.reset()
    },
    reset() {
      this.resetForm("form");
      // for (const key in this.data) {
      //   if (Object.hasOwnProperty.call(this.data, key)) {
      //     this.form[key] = this.data[key];
      //   }
      // }
      this.form = this.data


    },

    submitForm: debounce(function () {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id) {
            editCleaningProcess(this.form).then(response => {
              if (response.code == 200) {
                this.$emit("success");
                this.$message.success(response.msg);
                this.visible = false;
              } else {
                this.$message.error(response.msg);
              }
            })
          } else {
            if (this.type == 'position') {
              addPlace(this.form).then(response => {
                if (response.code == 200) {
                  this.$emit("success");
                  this.$message.success(response.msg);
                  this.visible = false;
                } else {
                  this.$message.error(response.msg);
                }
              })
            }
            if (this.type == 'checkpoint') {
              addCheckPoint(this.form).then(response => {
                if (response.code == 200) {
                  this.$emit("success");
                  this.$message.success(response.msg);
                  this.visible = false;
                } else {
                  this.$message.error(response.msg);
                }
              })
            }

          }
        }
      })
    }, 500, true),
    cancel() {
      this.reset();
      this.visible = false;
    },
  },
};
</script>

<style lang="scss" scoped>
@import "~@/assets/styles/dialog.scss";
</style>
